const fs = require('fs');
const path = require('path');

function req(filePath) {
  // 文件内容
  const fileText = fs.readFileSync(path.resolve(__dirname, filePath) , 'utf-8');

  // Function对象接受的参数，前面的参数都是function的参数，最后一个参数是函数的内容体
  const fn = new Function('exports', 'module', 'require', '__dirname', '__filename', fileText + '\n return module.exports');

  let module = { 
    exports: {}
  }

  return fn(module.exports, module, req, __dirname, __filename);

}


const str = require('./module_a.js'); // 同步引入
const str1 = req('./module_a.js');
/**
 * new Function('exports', 'module', 'require', '__dirname', '__filename', fileText + '\n return module.exports');
 * 等价于
 * function(exports, module, require, __dirname, __filename) {
 *    module.exports = 'this is module_a.js content';
 *    return module.exports;
 * }
 */
console.log(str);
console.log(str1);